package org.example.frame;

import lombok.extern.slf4j.Slf4j;
import org.example.function.Tool;
import org.example.listener.GlobalKeyListener;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Author Crane Resigned
 * Date: 2022-11-23 22:22:37
 *
 * @author Crane Resigned
 */
@Slf4j
public class MainFrame extends JFrame {

    /**
     * 日期格式化文本
     * Author: Crane Resigned
     * Date: 2022-11-23 23:07:03
     */
    private static final String FORMAT_DATE_STR = "yyMMddHHmmss";

    /**
     * 格式化文本
     * Author Crane Resigned
     * Date: 2022-11-23 22:00:41
     */
    public static JTextField formatText;

    /**
     * 结果显示标签
     * Author Crane Resigned
     * Date: 2022-11-23 22:00:35
     */
    public static JLabel resultLabel;

    public MainFrame() {

        this.setTitle("Generate Timestamp v2.0");
        this.setSize(400, 200);
        this.setLayout(null);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setUndecorated(true);
//        this.setOpacity(0.55f);

        //关闭按钮
        JButton closeButton = new JButton("退出");
        closeButton.setBounds(280, 0, 120, 30);
        closeButton.setFocusPainted(false);
        closeButton.setBackground(Color.white);
        closeButton.setBorderPainted(false);
        closeButton.setFont(new Font("宋体", Font.ITALIC, 14));
        closeButton.addActionListener(e -> System.exit(0));
        this.add(closeButton);

        JButton resetButton = new JButton("重置格式");
        resetButton.setBounds(280, 30, 120, 30);
        resetButton.setFocusPainted(false);
        resetButton.setBackground(Color.white);
        resetButton.setBorderPainted(false);
        resetButton.setFont(new Font("宋体", Font.ITALIC, 14));
        resetButton.addActionListener(e -> formatText.setText(FORMAT_DATE_STR));
        this.add(resetButton);

        //格式输入框
        formatText = new JTextField(FORMAT_DATE_STR);
        formatText.setBounds(10, 10, 200, 30);
        formatText.setBorder(null);
        formatText.setHorizontalAlignment(JTextField.CENTER);
        formatText.setFont(new Font("宋体", Font.ITALIC, 14));
        this.add(formatText);

        //结果标签
        resultLabel = new JLabel();
        resultLabel.setBounds(280, 120, 200, 30);
        resultLabel.setForeground(Color.red);
        resultLabel.setFont(new Font("宋体", Font.ITALIC, 14));
        resultLabel.setVisible(true);
        this.add(resultLabel);

        //主要功能按钮
        JButton mainButton = new JButton("Get Timestamp");
        mainButton.setBackground(Color.white);
        mainButton.setFocusPainted(false);
        mainButton.setBorderPainted(false);
        mainButton.setBounds(10, 100, 200, 30);
        mainButton.addActionListener(e -> {
            String resultStr = Tool.generateTime(formatText.getText());
            resultLabel.setText(Objects.isNull(resultStr) ? "错误格式，请重置" : "已复制至剪贴板");
            Tool.stick(resultStr);
            resultLabel.setVisible(true);
            //两秒后关闭显示
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    resultLabel.setVisible(false);
                }
            }, 2000);
        });
        this.add(mainButton);

        //是否启动键盘监听事件的按钮
        JToggleButton monitorButton = new JToggleButton("点击监听", false);
        monitorButton.setBounds(280, 60, 120, 30);
        monitorButton.setFocusPainted(false);
        monitorButton.setBackground(Color.white);
        monitorButton.setBorderPainted(false);
        monitorButton.setFont(new Font("宋体", Font.ITALIC, 14));
        monitorButton.addActionListener(e -> {
            //获取事件源
            JToggleButton source = (JToggleButton) e.getSource();
            if (source.isSelected()) {
                log.info("启用事件监听");
                GlobalScreen.addNativeKeyListener(GlobalKeyListener.getGlobalKeyListener());
            }
            if (!source.isSelected()) {
                log.info("关闭事件监听");
                GlobalScreen.removeNativeKeyListener(GlobalKeyListener.getGlobalKeyListener());
            }
        });
        this.add(monitorButton);
        //根据int键码获取键文本
        JLabel showKeyLabel = new JLabel("监听键：".concat(NativeKeyEvent.getKeyText(GlobalKeyListener.getEventKey())));
        showKeyLabel.setBounds(280, 90, 120, 30);
        showKeyLabel.setForeground(Color.blue);
        showKeyLabel.setFont(new Font("宋体", Font.ITALIC, 14));
        showKeyLabel.setHorizontalAlignment(JLabel.CENTER);
        this.add(showKeyLabel);
    }

}
